package com.dexilaonian.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.adpter.PietyAdapter;
import com.dexilaonian.bean.PietyBean;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.Okhttp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class Piety_card extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.base_topText)
    TextView baseTopText;
    @Bind(R.id.base_choose)
    TextView baseChoose;
    @Bind(R.id.tv_vv)
    TextView tvVv;
    @Bind(R.id.tv_balance)
    TextView tvBalance;
    @Bind(R.id.huise)
    ImageView huise;
    @Bind(R.id.zhangshu)
    TextView zhangshu;
    @Bind(R.id.rl_youhuijuan)
    RelativeLayout rlYouhuijuan;
    @Bind(R.id.huise2)
    ImageView huise2;
    @Bind(R.id.jifen)
    TextView jifen;
    @Bind(R.id.rl_jifen)
    RelativeLayout rlJifen;
    @Bind(R.id.piety_viewpager)
    ViewPager pietyViewpager;
    private List<View> bean_list = new ArrayList<>();
    private List<String> beans = new ArrayList<>();
    private String vip_url;

    @Override
    public int getLayoutID() {
        return R.layout.activity_piety_card;
    }

    @Override
    public void initView() {
    }

    @Override
    protected void initData() {
        vip_url = Constant.base_url + "/vipCard/list.do";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        baseChoose.setVisibility(View.VISIBLE);
        baseChoose.setText("添加");
        baseTopText.setText("孝心卡");
        pietyViewpager.setCurrentItem(0);
        init();
    }
    private void init() {
        showProgressBar(true);
        Map<String, String> map = new HashMap<>();
        Okhttp.postString(true, vip_url, map, new Okhttp.CallBac() {
            @Override
            public void onError(Call call, Exception e, String state, int id) {
                showProgressBar(false);
            }

            @Override
            public void onResponse(String response, int id) {
                showProgressBar(false);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 35) {
                       Intent intent = new Intent(Piety_card.this, ServiceActivity.class);
                        intent.putExtra("CONFIRM", "");
                        startActivity(intent);
                        return;
                    }
                    if (status == 0) {
                        String data = jsonObject.optString("data");
                        JSONArray jsonArray = new JSONArray(data);
                        final PietyBean pietyBean = new PietyBean();
                        for (int i = 0; i < jsonArray.length(); i++) {
                            View inflate1 = View.inflate(Piety_card.this, R.layout.view1, null);
                            ImageView ivPic = (ImageView) inflate1.findViewById(R.id.iv_pic);
                            TextView kaka = (TextView) inflate1.findViewById(R.id.tv_kaka);
                            TextView kaName = (TextView) inflate1.findViewById(R.id.tv_ka_name);
                            TextView kaNumber = (TextView) inflate1.findViewById(R.id.tv_ka_number);
                            TextView tvRelation = (TextView) inflate1.findViewById(R.id.tv_relation);
                            String piety = jsonArray.getString(i);
                            JSONObject object = new JSONObject(piety);
                            double score = Double.parseDouble(object.optString("score"));
                            double total_deposits = Double.parseDouble(object.optString("total_deposits"));
                            int couponCount = Integer.parseInt(object.optString("couponCount"));
                            String real_name = object.optString("real_name");
                            String VIP_card = object.optString("VIP_card");
                            int customer_id = Integer.parseInt(object.optString("customer_id"));
                            int VIPcard_id = Integer.parseInt(object.optString("VIPcard_id"));
                            String relation = object.optString("relation");
                            pietyBean.setScore(score);
                            pietyBean.setTotal_deposits(total_deposits);
                            pietyBean.setCouponCount(couponCount);
                            pietyBean.setReal_name(real_name);
                            pietyBean.setVIP_card(VIP_card);
                            pietyBean.setCustomer_id(customer_id);
                            pietyBean.setVIPcard_id(VIPcard_id);
                            pietyBean.setRelation(relation);
                            kaName.setText(pietyBean.getReal_name());
                            if (pietyBean.getRelation().equals("null")) {
                                tvRelation.setText("");
                                tvRelation.setBackground(null);
                            } else {
                                tvRelation.setText(pietyBean.getRelation());
                            }
                            kaNumber.setText(pietyBean.getVIP_card());
                            //把每一个卡片的钱保存数组中
                            //保留两位小数点 ”0.00"  保留 一位 “0.0”
                            String total_deposits_str = new DecimalFormat("0.00").format(total_deposits);
                            beans.add(total_deposits_str);
                            bean_list.add(inflate1);
                        }
                        PietyAdapter pietyAdapter = new PietyAdapter(Piety_card.this, bean_list);
                        pietyViewpager.setAdapter(pietyAdapter);
                        //默认第一个卡片 ，所以取第一个
                        tvBalance.setText(beans.get(0));
                        pietyViewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                            @Override
                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                            }

                            @Override
                            public void onPageSelected(int position) {
                                tvBalance.setText(beans.get(position));
                            }

                            @Override
                            public void onPageScrollStateChanged(int state) {
                            }
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onNoNetwork(String state) {
                showProgressBar(false);
            }
        });
    }

    @OnClick({R.id.back,R.id.base_topText, R.id.base_choose, R.id.rl_youhuijuan, R.id.rl_jifen})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
            case R.id.base_topText:
                finish();
                break;
            case R.id.base_choose:
                Intent intent = new Intent(Piety_card.this, ServiceActivity.class);
                intent.putExtra("CONFIRM", "");
                startActivity(intent);
                break;
            case R.id.rl_youhuijuan:
                startActivity(new Intent(Piety_card.this, CouponActivity.class));
                break;
            case R.id.rl_jifen:
                startActivity(new Intent(Piety_card.this, IntegralActivity.class));
                break;
        }
    }
}
